<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>医捷通</h1>
        <p>就医便捷，流程通畅,</p>
      </div>
      
      <el-form 
        ref="loginFormRef" 
        :model="loginForm" 
        :rules="loginRules" 
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            size="large"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            size="large"
            show-password
            clearable
          />
        </el-form-item>
        
        <el-form-item>
          <div class="login-options">
            <el-checkbox v-model="rememberPassword">
              <el-icon><Check /></el-icon>
              记住密码
            </el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            size="large" 
            class="login-button"
            :loading="loading"
            @click="handleLogin"
          >
            <el-icon v-if="!loading"><Right /></el-icon>
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <!-- 登录提示 -->

      <div class="login-tips">
        <p><strong>测试账号：</strong></p>
        <div class="account-list">
          <div class="account-item">
            <el-tag size="small" type="info">患者</el-tag>
            <span>用户名：patient1 密码：123456</span>
          </div>
          <div class="account-item">
            <el-tag size="small" type="info">患者</el-tag>
            <span>用户名：patient2 密码：123456</span>
          </div>
          <div class="account-item">
            <el-tag size="small" type="warning">医生</el-tag>
            <span>用户名：doctor1 密码：123456</span>
          </div>
          <div class="account-item">
            <el-tag size="small" type="danger">管理员</el-tag>
            <span>用户名：admin 密码：123456</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { mockAPI } from '../utils/mockData'

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const loginForm = reactive({
      username: '',
      password: ''
    })
    
    const rememberPassword = ref(false)
    const loading = ref(false)
    const loginFormRef = ref()
    
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
      ]
    }
    
    const handleLogin = async () => {
      try {
        await loginFormRef.value.validate()
        loading.value = true
        
        console.log('提交登录表单:', loginForm)
        
        const result = await mockAPI.login(loginForm.username, loginForm.password)
        
        console.log('登录结果:', result)
        
        // 保存用户信息
        localStorage.setItem('user', JSON.stringify(result.data))
        
        if (rememberPassword.value) {
          localStorage.setItem('rememberedUser', JSON.stringify({
            username: loginForm.username,
            password: loginForm.password
          }))
        } else {
          localStorage.removeItem('rememberedUser')
        }
        
        ElMessage.success('登录成功')
        router.push('/home')
      } catch (error) {
        console.error('登录错误:', error)
        ElMessage.error(error.message || '登录失败')
      } finally {
        loading.value = false
      }
    }
    
    // 检查是否有记住的用户信息
    const rememberedUser = localStorage.getItem('rememberedUser')
    if (rememberedUser) {
      const user = JSON.parse(rememberedUser)
      loginForm.username = user.username
      loginForm.password = user.password
      rememberPassword.value = true
    }
    
    return {
      loginForm,
      loginFormRef,
      loginRules,
      rememberPassword,
      loading,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-box {
  background: white;
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  width: 400px;
  max-width: 90vw;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  color: #409EFF;
  font-size: 32px;
  margin-bottom: 10px;
  font-weight: bold;
}

.login-header p {
  color: #666;
  font-size: 16px;
}

.login-form {
  margin-bottom: 20px;
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.login-options .el-checkbox {
  display: flex;
  align-items: center;
  gap: 5px;
}

.login-button {
  width: 100%;
  height: 50px;
  font-size: 18px;
  border-radius: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.login-tips {
  text-align: center;
  color: #999;
  font-size: 14px;
  line-height: 1.6;
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.account-list {
  margin-top: 10px;
}

.account-item {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 5px;
}

.account-item span {
  font-size: 12px;
}
</style> 